package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    /**
     * 登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //将密码进行md5加密
        String password = employee.getPassword();
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
//      根据;用户账号查询单个用户信息
        LambdaQueryWrapper<Employee> lam = new LambdaQueryWrapper<>();
        lam.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(lam);

        //先判断账号是否存在,再判断密码是否正确
        if (one == null || !one.getPassword().equals(s)) {
            return R.error("账号或密码错误!");
        }
        if (one.getStatus() == 0) {
            return R.error("该账户已被禁用!");
        }
        //登录成功将用户id存入session
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);

    }

    /**
     * 退出系统
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加员工
     *
     * @param employee
     * @param request
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Employee employee, HttpServletRequest request) {
        //设置创建和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //设置初始密码为123456并进行md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //设置创建人和修改人
        Long id = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(id);
        employee.setCreateUser(id);

        employeeService.save(employee);
        return R.success("添加成功!");

    }

    /**
     * 分页查询员工信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> list(@RequestParam int page, int pageSize, String name) {
        Page p = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> lam = new LambdaQueryWrapper<>();
        lam.like(StringUtils.isNotEmpty(name), Employee::getName, name)
                .orderByAsc(Employee::getUpdateTime);
        employeeService.page(p, lam);
        return R.success(p);
    }

    /**
     * 修改员工信息(编辑以及修改状态)
     * @param employee
     * @param request
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Employee employee,HttpServletRequest request){
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");

    }

    /**
     * 修改回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> show(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        if (byId != null){

        return R.success(byId);
        }
        return R.error("未查询到该员工信息!");
    }

    @DeleteMapping
    public R<String> deleteById(@RequestBody Employee employee){
        Long id=employee.getId();
        boolean b = employeeService.removeById(id);
        if (b){
        return R.success("删除成功!");
        }
        return R.error("删除失败!");
    }

}
